home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / byacc 1.8.2 / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-04  |  25.6 KB  |  1,454 lines  |  [TEXT/R*ch]

  1. #include "defs.h"
  2.  
  3. static int nvectors;
  4. static int nentries;
  5. static short **froms;
  6. static short **tos;
  7. static short *tally;
  8. static short *width;
  9. static short *state_count;
  10. static short *order;
  11. static short *base;
  12. static short *pos;
  13. static int maxtable;
  14. static short *table;
  15. static short *check;
  16. static int lowzero;
  17. static int high;
  18.  
  19.  
  20. static void free_itemsets _P_((void));
  21. static void free_shifts _P_((void));
  22. static void free_reductions _P_((void));
  23. static void output_stored_text _P_((void));
  24. static void output_defines _P_((void));
  25. static void write_num _P_((int value));
  26. static void start_num_array _P_((char *name, int indent, int first));
  27. static void start_string_array _P_((char *name, int nl));
  28. static void end_num_array _P_((void));
  29. static void end_string_array _P_((int nl));
  30. static void start_define _P_((FILE *file));
  31. static void end_define _P_((FILE *file, int value));
  32. static void write_define _P_((char *name, int value));
  33. static void write_null _P_((void));
  34. static void output_rule_data _P_((void));
  35. static void output_yydefred _P_((void));
  36. static void output_actions _P_((void));
  37. static void token_actions _P_((void));
  38. static void goto_actions _P_((void));
  39. static int default_goto _P_((int symbol));
  40. static void save_column _P_((int symbol, int default_state));
  41. static void sort_actions _P_((void));
  42. static void pack_table _P_((void));
  43. static int matching_vector _P_((int vector));
  44. static int pack_vector _P_((int vector));
  45. static void output_base _P_((void));
  46. static void output_table _P_((void));
  47. static void output_check _P_((void));
  48. static int is_C_identifier _P_((char *name));
  49. static void output_debug _P_((void));
  50. static void output_stype _P_((void));
  51. static void output_trailing_text _P_((void));
  52. static void output_semantic_actions _P_((void));
  53.  
  54.  
  55. #if __STDC__
  56. void output(void)
  57. #else
  58. void output()
  59. #endif
  60. {
  61.     free_itemsets();
  62.     free_shifts();
  63.     free_reductions();
  64.     output_stored_text();
  65.     output_defines();
  66.     output_rule_data();
  67.     output_yydefred();
  68.     output_actions();
  69.     free_parser();
  70.     output_debug();
  71.     output_stype();
  72.     if (rflag) write_section(tables);
  73.     write_section(header);
  74.     if (language == C)
  75.     output_trailing_text();
  76.     write_section(body);
  77.     output_semantic_actions();
  78.     write_section(trailer);
  79.     if (language == PERL)
  80.     output_trailing_text();
  81.     free_derives();
  82.     free_nullable();
  83. }
  84.  
  85.  
  86. #if __STDC__
  87. static void write_num(int value)
  88. #else
  89. static void write_num(value)
  90. int value;
  91. #endif
  92. {
  93.     switch (language)
  94.     {
  95.     case PERL:
  96.     fprintf(output_file, "%5d,", value);
  97.     break;
  98.     case C:
  99.     fprintf(output_file, "%5d,", value);
  100.     break;
  101.     }
  102. }
  103.  
  104. #if __STDC__
  105. static void start_num_array(char *name, int indent, int first)
  106. #else
  107. static void start_num_array(name, indent, first)
  108. char *name;
  109. int indent, first;
  110. #endif
  111. {
  112.     switch (language)
  113.     {
  114.     case PERL:
  115.     fprintf(output_file, "@%s%s = (%*d,",
  116.         symbol_prefix, name, indent+7, first);
  117.     break;
  118.     case C:
  119.     fprintf(output_file, "short %s%s[] = {%*d,",
  120.         symbol_prefix, name, indent, first);
  121.     break;
  122.     }
  123. }
  124.  
  125. #if __STDC__
  126. static void start_string_array(char *name, int nl)
  127. #else
  128. static void start_string_array(name, nl)
  129. char *name;
  130. int nl;
  131. #endif
  132. {
  133.     switch (language)
  134.     {
  135.     case PERL:
  136.     fprintf(output_file, "@%s%s = (%s",
  137.         symbol_prefix, name, nl ? "\n" : "");
  138.     break;
  139.     case C:
  140.     fprintf(output_file, "char *%s%s[] = {%s",
  141.         symbol_prefix, name, nl ? "\n": "");
  142.     break;
  143.     }
  144. }
  145.  
  146. #if __STDC_
  147. static void end_num_array(void)
  148. #else
  149. static void end_num_array()
  150. #endif
  151. {
  152.     switch (language)
  153.     {
  154.     case PERL:
  155.     fprintf(output_file, "\n);\n");
  156.     break;
  157.     case C:
  158.     fprintf(output_file, "\n};\n");
  159.     break;
  160.     }
  161. }
  162.  
  163. #if __STDC__
  164. static void end_string_array(int nl)
  165. #else
  166. static void end_string_array(nl)
  167. int nl;
  168. #endif
  169. {
  170.     switch (language)
  171.     {
  172.     case PERL:
  173.     fprintf(output_file, "%s);\n", nl ? "\n" : "");
  174.     break;
  175.     case C:
  176.     fprintf(output_file, "%s};\n", nl ? "\n" : "");
  177.     break;
  178.     }
  179. }
  180.  
  181. #if __STDC__
  182. static void start_define(FILE *file)
  183. #else
  184. static void start_define(file)
  185. FILE *file;
  186. #endif
  187. {
  188.     switch (language)
  189.     {
  190.     case PERL:
  191.     fprintf(file, "$");
  192.     break;
  193.     case C:
  194.     fprintf(file, "#define ");
  195.     break;
  196.     }
  197. }
  198.  
  199. #if __STDC__
  200. static void end_define(FILE *file, int value)
  201. #else
  202. static void end_define(file, value)
  203. FILE* file;
  204. int value;
  205. #endif
  206. {
  207.     if (language == PERL)
  208.     fprintf(file, "=%d;\n", value);
  209.     else
  210.     fprintf(file, " %d\n", value);
  211. }
  212.  
  213. #if __STDC_
  214. static void write_define(char *name, int value)
  215. #else
  216. static void write_define(name, value)
  217. char* name;
  218. int value;
  219. #endif
  220. {
  221.     start_define(code_file);
  222.     fprintf(code_file, "%s%s", define_prefix, name);
  223.     end_define(code_file, value);
  224. }
  225.  
  226. #if __STDC_
  227. static void write_null(void)
  228. #else
  229. static void write_null()
  230. #endif
  231. {
  232.     switch (language)
  233.     {
  234.     case PERL:
  235.     fprintf(output_file, "'',");
  236.     break;
  237.     case C:
  238.     fprintf(output_file, "0,");
  239.     break;
  240.     }
  241. }
  242.  
  243. #if __STDC__
  244. static void output_rule_data(void)
  245. #else
  246. static void output_rule_data()
  247. #endif
  248. {
  249.     register int i;
  250.     register int j;
  251.  
  252.   
  253.     start_num_array("lhs", 42, symbol_value[start_symbol]);
  254.  
  255.     j = 10;
  256.     for (i = 3; i < nrules; i++)
  257.     {
  258.     if (j >= 10)
  259.     {
  260.         if (!rflag) ++outline;
  261.         putc('\n', output_file);
  262.         j = 1;
  263.     }
  264.     else
  265.         ++j;
  266.  
  267.     write_num(symbol_value[rlhs[i]]);
  268.     }
  269.     if (!rflag) outline += 2;
  270.     end_num_array();
  271.   
  272.     start_num_array("len", 42, 2);
  273.  
  274.     j = 10;
  275.     for (i = 3; i < nrules; i++)
  276.     {
  277.     if (j >= 10)
  278.     {
  279.         if (!rflag) ++outline;
  280.         putc('\n', output_file);
  281.         j = 1;
  282.     }
  283.     else
  284.       j++;
  285.  
  286.     write_num(rrhs[i + 1] - rrhs[i] - 1);
  287.     }
  288.     if (!rflag) outline += 2;
  289.     end_num_array();
  290. }
  291.  
  292.  
  293. #if __STDC__
  294. static void output_yydefred(void)
  295. #else
  296. static void output_yydefred()
  297. #endif
  298. {
  299.     register int i, j;
  300.  
  301.     start_num_array("defred", 39,
  302.             (defred[0] ? defred[0] - 2 : 0));
  303.  
  304.     j = 10;
  305.     for (i = 1; i < nstates; i++)
  306.     {
  307.     if (j < 10)
  308.         ++j;
  309.     else
  310.     {
  311.         if (!rflag) ++outline;
  312.         putc('\n', output_file);
  313.         j = 1;
  314.     }
  315.  
  316.     write_num(defred[i] ? defred[i] - 2 : 0);
  317.     }
  318.  
  319.     if (!rflag) outline += 2;
  320.     end_num_array();
  321. }
  322.  
  323.  
  324. #if __STDC__
  325. static void output_actions(void)
  326. #else
  327. static void output_actions()
  328. #endif
  329. {
  330.     nvectors = 2*nstates + nvars;
  331.  
  332.     froms = NEW2(nvectors, short *);
  333.     tos = NEW2(nvectors, short *);
  334.     tally = NEW2(nvectors, short);
  335.     width = NEW2(nvectors, short);
  336.  
  337.     token_actions();
  338.     FREE(lookaheads);
  339.     FREE(LA);
  340.     FREE(LAruleno);
  341.     FREE(accessing_symbol);
  342.  
  343.     goto_actions();
  344.     FREE(goto_map + ntokens);
  345.     FREE(from_state);
  346.     FREE(to_state);
  347.  
  348.     sort_actions();
  349.     pack_table();
  350.     output_base();
  351.     output_table();
  352.     output_check();
  353. }
  354.  
  355.  
  356. #if __STDC__
  357. static void token_actions(void)
  358. #else
  359. static void token_actions()
  360. #endif
  361. {
  362.     register int i, j;
  363.     register int shiftcount, reducecount;
  364.     register int max, min;
  365.     register short *actionrow, *r, *s;
  366.     register action *p;
  367.  
  368.     actionrow = NEW2(2*ntokens, short);
  369.     for (i = 0; i < nstates; ++i)
  370.     {
  371.     if (parser[i])
  372.     {
  373.         for (j = 0; j < 2*ntokens; ++j)
  374.         actionrow[j] = 0;
  375.  
  376.         shiftcount = 0;
  377.         reducecount = 0;
  378.         for (p = parser[i]; p; p = p->next)
  379.         {
  380.         if (p->suppressed == 0)
  381.         {
  382.             if (p->action_code == SHIFT)
  383.             {
  384.             ++shiftcount;
  385.             actionrow[p->symbol] = p->number;
  386.             }
  387.             else if (p->action_code == REDUCE && p->number != defred[i])
  388.             {
  389.             ++reducecount;
  390.             actionrow[p->symbol + ntokens] = p->number;
  391.             }
  392.         }
  393.         }
  394.  
  395.         tally[i] = shiftcount;
  396.         tally[nstates+i] = reducecount;
  397.         width[i] = 0;
  398.         width[nstates+i] = 0;
  399.         if (shiftcount > 0)
  400.         {
  401.         froms[i] = r = NEW2(shiftcount, short);
  402.         tos[i] = s = NEW2(shiftcount, short);
  403.         min = MAXSHORT;
  404.         max = 0;
  405.         for (j = 0; j < ntokens; ++j)
  406.         {
  407.             if (actionrow[j])
  408.             {
  409.             if (min > symbol_value[j])
  410.                 min = symbol_value[j];
  411.             if (max < symbol_value[j])
  412.                 max = symbol_value[j];
  413.             *r++ = symbol_value[j];
  414.             *s++ = actionrow[j];
  415.             }
  416.         }
  417.         width[i] = max - min + 1;
  418.         }
  419.         if (reducecount > 0)
  420.         {
  421.         froms[nstates+i] = r = NEW2(reducecount, short);
  422.         tos[nstates+i] = s = NEW2(reducecount, short);
  423.         min = MAXSHORT;
  424.         max = 0;
  425.         for (j = 0; j < ntokens; ++j)
  426.         {
  427.             if (actionrow[ntokens+j])
  428.             {
  429.             if (min > symbol_value[j])
  430.                 min = symbol_value[j];
  431.             if (max < symbol_value[j])
  432.                 max = symbol_value[j];
  433.             *r++ = symbol_value[j];
  434.             *s++ = actionrow[ntokens+j] - 2;
  435.             }
  436.         }
  437.         width[nstates+i] = max - min + 1;
  438.         }
  439.     }
  440.     }
  441.     FREE(actionrow);
  442. }
  443.  
  444. #if __STDC__
  445. static void goto_actions(void)
  446. #else
  447. static void goto_actions()
  448. #endif
  449. {
  450.     register int i, j, k;
  451.  
  452.     state_count = NEW2(nstates, short);
  453.  
  454.     k = default_goto(start_symbol + 1);
  455.     start_num_array("dgoto", 40, k);
  456.     save_column(start_symbol + 1, k);
  457.  
  458.     j = 10;
  459.     for (i = start_symbol + 2; i < nsyms; i++)
  460.     {
  461.     if (j >= 10)
  462.     {
  463.         if (!rflag) ++outline;
  464.         putc('\n', output_file);
  465.         j = 1;
  466.     }
  467.     else
  468.         ++j;
  469.  
  470.     k = default_goto(i);
  471.     write_num(k);
  472.     save_column(i, k);
  473.     }
  474.  
  475.     if (!rflag) outline += 2;
  476.     end_num_array();
  477.     FREE(state_count);
  478. }
  479.  
  480. #if __STDC__
  481. static int default_goto(int symbol)
  482. #else
  483. static int default_goto(symbol)
  484. int symbol;
  485. #endif
  486. {
  487.     register int i;
  488.     register int m;
  489.     register int n;
  490.     register int default_state;
  491.     register int max;
  492.  
  493.     m = goto_map[symbol];
  494.     n = goto_map[symbol + 1];
  495.  
  496.     if (m == n) return (0);
  497.  
  498.     for (i = 0; i < nstates; i++)
  499.     state_count[i] = 0;
  500.  
  501.     for (i = m; i < n; i++)
  502.     state_count[to_state[i]]++;
  503.  
  504.     max = 0;
  505.     default_state = 0;
  506.     for (i = 0; i < nstates; i++)
  507.     {
  508.     if (state_count[i] > max)
  509.     {
  510.         max = state_count[i];
  511.         default_state = i;
  512.     }
  513.     }
  514.  
  515.     return (default_state);
  516. }
  517.  
  518.  
  519. #if __STDC__
  520. static void save_column(int symbol, int default_state)
  521. #else
  522. static void save_column(symbol, default_state)
  523. int symbol;
  524. int default_state;
  525. #endif
  526. {
  527.     register int i;
  528.     register int m;
  529.     register int n;
  530.     register short *sp;
  531.     register short *sp1;
  532.     register short *sp2;
  533.     register int count;
  534.     register int symno;
  535.  
  536.     m = goto_map[symbol];
  537.     n = goto_map[symbol + 1];
  538.  
  539.     count = 0;
  540.     for (i = m; i < n; i++)
  541.     {
  542.     if (to_state[i] != default_state)
  543.         ++count;
  544.     }
  545.     if (count == 0) return;
  546.  
  547.     symno = symbol_value[symbol] + 2*nstates;
  548.  
  549.     froms[symno] = sp1 = sp = NEW2(count, short);
  550.     tos[symno] = sp2 = NEW2(count, short);
  551.  
  552.     for (i = m; i < n; i++)
  553.     {
  554.     if (to_state[i] != default_state)
  555.     {
  556.         *sp1++ = from_state[i];
  557.         *sp2++ = to_state[i];
  558.     }
  559.     }
  560.  
  561.     tally[symno] = count;
  562.     width[symno] = sp1[-1] - sp[0] + 1;
  563. }
  564.  
  565. #if __STDC__
  566. static void sort_actions(void)
  567. #else
  568. static void sort_actions()
  569. #endif
  570. {
  571.   register int i;
  572.   register int j;
  573.   register int k;
  574.   register int t;
  575.   register int w;
  576.  
  577.   order = NEW2(nvectors, short);
  578.   nentries = 0;
  579.  
  580.   for (i = 0; i < nvectors; i++)
  581.     {
  582.       if (tally[i] > 0)
  583.     {
  584.       t = tally[i];
  585.       w = width[i];
  586.       j = nentries - 1;
  587.  
  588.       while (j >= 0 && (width[order[j]] < w))
  589.         j--;
  590.  
  591.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  592.         j--;
  593.  
  594.       for (k = nentries - 1; k > j; k--)
  595.         order[k + 1] = order[k];
  596.  
  597.       order[j + 1] = i;
  598.       nentries++;
  599.     }
  600.     }
  601. }
  602.  
  603.  
  604. #if __STDC__
  605. static void pack_table(void)
  606. #else
  607. static void pack_table()
  608. #endif
  609. {
  610.     register int i;
  611.     register int place;
  612.     register int state;
  613.  
  614.     base = NEW2(nvectors, short);
  615.     pos = NEW2(nentries, short);
  616.  
  617.     maxtable = 1000;
  618.     table = NEW2(maxtable, short);
  619.     check = NEW2(maxtable, short);
  620.  
  621.     lowzero = 0;
  622.     high = 0;
  623.  
  624.     for (i = 0; i < maxtable; i++)
  625.     check[i] = -1;
  626.  
  627.     for (i = 0; i < nentries; i++)
  628.     {
  629.     state = matching_vector(i);
  630.  
  631.     if (state < 0)
  632.         place = pack_vector(i);
  633.     else
  634.         place = base[state];
  635.  
  636.     pos[i] = place;
  637.     base[order[i]] = place;
  638.     }
  639.  
  640.     for (i = 0; i < nvectors; i++)
  641.     {
  642.     if (froms[i])
  643.         FREE(froms[i]);
  644.     if (tos[i])
  645.         FREE(tos[i]);
  646.     }
  647.  
  648.     FREE(froms);
  649.     FREE(tos);
  650.     FREE(pos);
  651. }
  652.  
  653.  
  654. /*  The function matching_vector determines if the vector specified by    */
  655. /*  the input parameter matches a previously considered    vector.     The    */
  656. /*  test at the start of the function checks if the vector represents    */
  657. /*  a row of shifts over terminal symbols or a row of reductions, or a    */
  658. /*  column of shifts over a nonterminal symbol.     Berkeley Yacc does not    */
  659. /*  check if a column of shifts over a nonterminal symbols matches a    */
  660. /*  previously considered vector.  Because of the nature of LR parsing    */
  661. /*  tables, no two columns can match.  Therefore, the only possible    */
  662. /*  match would be between a row and a column.    Such matches are    */
  663. /*  unlikely.  Therefore, to save time, no attempt is made to see if a    */
  664. /*  column matches a previously considered vector.            */
  665. /*                                    */
  666. /*  Matching_vector is poorly designed.     The test could easily be made    */
  667. /*  faster.  Also, it depends on the vectors being in a specific    */
  668. /*  order.                                */
  669.  
  670. #if __STDC__
  671. static int matching_vector(int vector)
  672. #else
  673. static int matching_vector(vector)
  674. int vector;
  675. #endif
  676. {
  677.     register int i;
  678.     register int j;
  679.     register int k;
  680.     register int t;
  681.     register int w;
  682.     register int match;
  683.     register int prev;
  684.  
  685.     i = order[vector];
  686.     if (i >= 2*nstates)
  687.     return (-1);
  688.  
  689.     t = tally[i];
  690.     w = width[i];
  691.  
  692.     for (prev = vector - 1; prev >= 0; prev--)
  693.     {
  694.     j = order[prev];
  695.     if (width[j] != w || tally[j] != t)
  696.         return (-1);
  697.  
  698.     match = 1;
  699.     for (k = 0; match && k < t; k++)
  700.     {
  701.         if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  702.         match = 0;
  703.     }
  704.  
  705.     if (match)
  706.         return (j);
  707.     }
  708.  
  709.     return (-1);
  710. }
  711.  
  712.  
  713.  
  714. #if __STDC__
  715. static int pack_vector(int vector)
  716. #else
  717. static int pack_vector(vector)
  718. int vector;
  719. #endif
  720. {
  721.     register int i, j, k, l;
  722.     register int t;
  723.     register int loc;
  724.     register int ok;
  725.     register short *from;
  726.     register short *to;
  727.     int newmax;
  728.  
  729.     i = order[vector];
  730.     t = tally[i];
  731.     assert(t);
  732.  
  733.     from = froms[i];
  734.     to = tos[i];
  735.  
  736.     j = lowzero - from[0];
  737.     for (k = 1; k < t; ++k)
  738.     if (lowzero - from[k] > j)
  739.         j = lowzero - from[k];
  740.     for (;; ++j)
  741.     {
  742.     if (j == 0)
  743.         continue;
  744.     ok = 1;
  745.     for (k = 0; ok && k < t; k++)
  746.     {
  747.         loc = j + from[k];
  748.         if (loc >= maxtable)
  749.         {
  750.         if (loc >= MAXTABLE)
  751.             fatal("maximum table size exceeded");
  752.  
  753.         newmax = maxtable;
  754.         do { newmax += 200; } while (newmax <= loc);
  755.         table = (short *) REALLOC(table, newmax*sizeof(short));
  756.         check = (short *) REALLOC(check, newmax*sizeof(short));
  757.         for (l    = maxtable; l < newmax; ++l)
  758.         {
  759.             table[l] = 0;
  760.             check[l] = -1;
  761.         }
  762.         maxtable = newmax;
  763.         }
  764.  
  765.         if (check[loc] != -1)
  766.         ok = 0;
  767.     }
  768.     for (k = 0; ok && k < vector; k++)
  769.     {
  770.         if (pos[k] == j)
  771.         ok = 0;
  772.     }
  773.     if (ok)
  774.     {
  775.         for (k = 0; k < t; k++)
  776.         {
  777.         loc = j + from[k];
  778.         table[loc] = to[k];
  779.         check[loc] = from[k];
  780.         if (loc > high) high = loc;
  781.         }
  782.  
  783.         while (check[lowzero] != -1)
  784.         ++lowzero;
  785.  
  786.         return (j);
  787.     }
  788.     }
  789. }
  790.  
  791.  
  792.  
  793. #if __STDC__
  794. static void output_base(void)
  795. #else
  796. static void output_base()
  797. #endif
  798. {
  799.     register int i, j;
  800.  
  801.     start_num_array("sindex", 39, base[0]);
  802.  
  803.     j = 10;
  804.     for (i = 1; i < nstates; i++)
  805.     {
  806.     if (j >= 10)
  807.     {
  808.         if (!rflag) ++outline;
  809.         putc('\n', output_file);
  810.         j = 1;
  811.     }
  812.     else
  813.         ++j;
  814.  
  815.     write_num(base[i]);
  816.     }
  817.  
  818.     if (!rflag) outline += 2;
  819.     end_num_array();
  820.  
  821.     start_num_array("rindex", 39, base[nstates]);
  822.  
  823.     j = 10;
  824.     for (i = nstates + 1; i < 2*nstates; i++)
  825.     {
  826.     if (j >= 10)
  827.     {
  828.         if (!rflag) ++outline;
  829.         putc('\n', output_file);
  830.         j = 1;
  831.     }
  832.     else
  833.         ++j;
  834.  
  835.     write_num(base[i]);
  836.     }
  837.  
  838.     if (!rflag) outline += 2;
  839.     end_num_array();
  840.   
  841.     start_num_array("gindex", 39, base[2*nstates]);
  842.  
  843.     j = 10;
  844.     for (i = 2*nstates + 1; i < nvectors - 1; i++)
  845.     {
  846.     if (j >= 10)
  847.     {
  848.         if (!rflag) ++outline;
  849.         putc('\n', output_file);
  850.         j = 1;
  851.     }
  852.     else
  853.         ++j;
  854.  
  855.     write_num(base[i]);
  856.     }
  857.  
  858.     if (!rflag) outline += 2;
  859.     end_num_array();
  860.     FREE(base);
  861. }
  862.  
  863.  
  864.  
  865. #if __STDC__
  866. static void output_table(void)
  867. #else
  868. static void output_table()
  869. #endif
  870. {
  871.     register int i;
  872.     register int j;
  873.  
  874.     ++outline;
  875.     write_define("TABLESIZE", high);
  876.     start_num_array("table", 40, table[0]);
  877.  
  878.     j = 10;
  879.     for (i = 1; i <= high; i++)
  880.     {
  881.     if (j >= 10)
  882.     {
  883.         if (!rflag) ++outline;
  884.         putc('\n', output_file);
  885.         j = 1;
  886.     }
  887.     else
  888.         ++j;
  889.  
  890.     write_num(table[i]);
  891.     }
  892.  
  893.     if (!rflag) outline += 2;
  894.     end_num_array();
  895.     FREE(table);
  896. }
  897.  
  898.  
  899.  
  900. #if __STDC__
  901. static void output_check(void)
  902. #else
  903. static void output_check()
  904. #endif
  905. {
  906.     register int i;
  907.     register int j;
  908.  
  909.     start_num_array("check", 40, check[0]);
  910.  
  911.     j = 10;
  912.     for (i = 1; i <= high; i++)
  913.     {
  914.     if (j >= 10)
  915.     {
  916.         if (!rflag) ++outline;
  917.         putc('\n', output_file);
  918.         j = 1;
  919.     }
  920.     else
  921.         ++j;
  922.  
  923.     write_num(check[i]);
  924.     }
  925.  
  926.     if (!rflag) outline += 2;
  927.     end_num_array();
  928.     FREE(check);
  929. }
  930.  
  931.  
  932. #if __STDC__
  933. static int is_C_identifier(char *name)
  934. #else
  935. static int is_C_identifier(name)
  936. char *name;
  937. #endif
  938. {
  939.     register char *s;
  940.     register int c;
  941.  
  942.     s = name;
  943.     c = *s;
  944.     if (c == '"')
  945.     {
  946.     c = *++s;
  947.     if (!isalpha(c) && c != '_' && c != '$')
  948.         return (0);
  949.     else if (language==PERL && c == '$')
  950.         return (0);
  951.     while ((c = *++s) != '"')
  952.     {
  953.         if (!isalnum(c) && c != '_' && c != '$')
  954.         return (0);
  955.         else if (language==PERL && c == '$')
  956.         return (0);
  957.     }
  958.     return (1);
  959.     }
  960.  
  961.     if (!isalpha(c) && c != '_' && c != '$')
  962.     return (0);
  963.     else if (language==PERL && c == '$')
  964.     return (0);
  965.     while (c = *++s)
  966.     {
  967.     if (!isalnum(c) && c != '_' && c != '$')
  968.         return (0);
  969.     else if (language==PERL && c == '$')
  970.         return (0);
  971.     }
  972.     return (1);
  973. }
  974.  
  975.  
  976. #if __STDC__
  977. static void output_defines(void)
  978. #else
  979. static void output_defines()
  980. #endif
  981. {
  982.     register int c, i;
  983.     register char *s;
  984.  
  985.     for (i = 2; i < ntokens; ++i)
  986.     {
  987.     s = symbol_name[i];
  988.     if (is_C_identifier(s))
  989.     {
  990.         start_define(code_file);
  991.         if (dflag)
  992.         start_define(defines_file);
  993.         c = *s;
  994.         if (c == '"')
  995.         {
  996.         while ((c = *++s) != '"')
  997.         {
  998.             putc(c, code_file);
  999.             if (dflag) putc(c, defines_file);
  1000.         }
  1001.         }
  1002.         else
  1003.         {
  1004.         do
  1005.         {
  1006.             putc(c, code_file);
  1007.             if (dflag) putc(c, defines_file);
  1008.         }
  1009.         while (c = *++s);
  1010.         }
  1011.         ++outline;
  1012.         end_define(code_file, symbol_value[i]);
  1013.         if (dflag)
  1014.         end_define(defines_file, symbol_value[i]);
  1015.     }
  1016.     }
  1017.  
  1018.     ++outline;
  1019.     write_define("ERRCODE", symbol_value[1]);
  1020.  
  1021.     if (dflag && unionized && language == C)
  1022.     {
  1023.     fclose(union_file);
  1024.     union_file = fopen(union_file_name, "r");
  1025.     if (union_file == NULL) open_error(union_file_name);
  1026.     while ((c = getc(union_file)) != EOF)
  1027.         putc(c, defines_file);
  1028.     fprintf(defines_file, " %sSTYPE;\nextern %sSTYPE %slval;\n",
  1029.         define_prefix, define_prefix, symbol_prefix);
  1030.     }
  1031. }
  1032.  
  1033.  
  1034. #if __STDC__
  1035. static void output_stored_text(void)
  1036. #else
  1037. static void output_stored_text()
  1038. #endif
  1039. {
  1040.     register int c;
  1041.     register FILE *in, *out;
  1042.  
  1043.     fclose(text_file);
  1044.     text_file = fopen(text_file_name, "r");
  1045.     if (text_file == NULL)
  1046.     open_error(text_file_name);
  1047.     in = text_file;
  1048.     if ((c = getc(in)) == EOF)
  1049.     return;
  1050.     out = code_file;
  1051.     if (c ==  '\n')
  1052.     ++outline;
  1053.     putc(c, out);
  1054.     while ((c = getc(in)) != EOF)
  1055.     {
  1056.     if (c == '\n')
  1057.         ++outline;
  1058.     putc(c, out);
  1059.     }
  1060.     if (!lflag)
  1061.     fprintf(out, line_format, ++outline + 1, code_file_name);
  1062. }
  1063.  
  1064.  
  1065. #if __STDC__
  1066. static void output_debug(void)
  1067. #else
  1068. static void output_debug()
  1069. #endif
  1070. {
  1071.     register int i, j, k, max;
  1072.     char **symnam, *s;
  1073.  
  1074.     ++outline;
  1075.     write_define("FINAL", final_state);
  1076.     outline += 3;
  1077.     fprintf(code_file, "#ifndef %sDEBUG\n#define %sDEBUG %d\n#endif\n",
  1078.         define_prefix, define_prefix, tflag);
  1079.     if (rflag)
  1080.     fprintf(output_file, "#ifndef %sDEBUG\n#define %sDEBUG %d\n#endif\n",
  1081.         define_prefix, define_prefix, tflag);
  1082.  
  1083.     max = 0;
  1084.     for (i = 2; i < ntokens; ++i)
  1085.     if (symbol_value[i] > max)
  1086.         max = symbol_value[i];
  1087.     ++outline;
  1088.     write_define("MAXTOKEN", max);
  1089.  
  1090.     symnam = (char **) MALLOC((max+1)*sizeof(char *));
  1091.  
  1092.     /* Note that it is    not necessary to initialize the element        */
  1093.     /* symnam[max].                            */
  1094.     for (i = 0; i < max; ++i)
  1095.     symnam[i] = 0;
  1096.     for (i = ntokens - 1; i >= 2; --i)
  1097.     symnam[symbol_value[i]] = symbol_name[i];
  1098.     symnam[0] = "end-of-file";
  1099.  
  1100.     if (!rflag) ++outline;
  1101.     fprintf(output_file, "#if %sDEBUG\n", define_prefix);
  1102.     start_string_array("name", 0);
  1103.     j = 80;
  1104.     for (i = 0; i <= max; ++i)
  1105.     {
  1106.     if (s = symnam[i])
  1107.     {
  1108.         if (s[0] == '"')
  1109.         {
  1110.         k = 7;
  1111.         while (*++s != '"')
  1112.         {
  1113.             ++k;
  1114.             if (*s == '\\')
  1115.             {
  1116.             k += 2;
  1117.             if (*++s == '\\')
  1118.                 ++k;
  1119.             }
  1120.         }
  1121.         j += k;
  1122.         if (j > 80)
  1123.         {
  1124.             if (!rflag) ++outline;
  1125.             putc('\n', output_file);
  1126.             j = k;
  1127.         }
  1128.         fprintf(output_file, "\"\\\"");
  1129.         s = symnam[i];
  1130.         while (*++s != '"')
  1131.         {
  1132.             if (*s == '\\')
  1133.             {
  1134.             fprintf(output_file, "\\\\");
  1135.             if (*++s == '\\')
  1136.                 fprintf(output_file, "\\\\");
  1137.             else
  1138.                 putc(*s, output_file);
  1139.             }
  1140.             else
  1141.             putc(*s, output_file);
  1142.         }
  1143.         fprintf(output_file, "\\\"\",");
  1144.         }
  1145.         else if (s[0] == '\'')
  1146.         {
  1147.         if (s[1] == '"')
  1148.         {
  1149.             j += 7;
  1150.             if (j > 80)
  1151.             {
  1152.             if (!rflag) ++outline;
  1153.             putc('\n', output_file);
  1154.             j = 7;
  1155.             }
  1156.             fprintf(output_file, "\"'\\\"'\",");
  1157.         }
  1158.         else
  1159.         {
  1160.             k = 5;
  1161.             while (*++s != '\'')
  1162.             {
  1163.             ++k;
  1164.             if (*s == '\\')
  1165.             {
  1166.                 k += 2;
  1167.                 if (*++s == '\\')
  1168.                 ++k;
  1169.             }
  1170.             }
  1171.             j += k;
  1172.             if (j > 80)
  1173.             {
  1174.             if (!rflag) ++outline;
  1175.             putc('\n', output_file);
  1176.             j = k;
  1177.             }
  1178.             fprintf(output_file, "\"'");
  1179.             s = symnam[i];
  1180.             while (*++s != '\'')
  1181.             {
  1182.             if (*s == '\\')
  1183.             {
  1184.                 fprintf(output_file, "\\\\");
  1185.                 if (*++s == '\\')
  1186.                 fprintf(output_file, "\\\\");
  1187.                 else
  1188.                 putc(*s, output_file);
  1189.             }
  1190.             else
  1191.                 putc(*s, output_file);
  1192.             }
  1193.             fprintf(output_file, "'\",");
  1194.         }
  1195.         }
  1196.         else
  1197.         {
  1198.         k = strlen(s) + 3;
  1199.         j += k;
  1200.         if (j > 80)
  1201.         {
  1202.             if (!rflag) ++outline;
  1203.             putc('\n', output_file);
  1204.             j = k;
  1205.         }
  1206.         putc('"', output_file);
  1207.         do { putc(*s, output_file); } while (*++s);
  1208.         fprintf(output_file, "\",");
  1209.         }
  1210.     }
  1211.     else
  1212.     {
  1213.         j += 2;
  1214.         if (j > 80)
  1215.         {
  1216.         if (!rflag) ++outline;
  1217.         putc('\n', output_file);
  1218.         j = 2;
  1219.         }
  1220.         write_null();
  1221.     }
  1222.     }
  1223.     if (!rflag) outline += 2;
  1224.     end_string_array(1);
  1225.     FREE(symnam);
  1226.  
  1227.     if (!rflag) ++outline;
  1228.     start_string_array("rule", 1);
  1229.     for (i = 2; i < nrules; ++i)
  1230.     {
  1231.     if (language == PERL && symbol_name[rlhs[i]][0] == '$')
  1232.         fprintf(output_file, "\"\\%s :", symbol_name[rlhs[i]]);
  1233.     else
  1234.         fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
  1235.     for (j = rrhs[i]; ritem[j] > 0; ++j)
  1236.     {
  1237.         s = symbol_name[ritem[j]];
  1238.         if (s[0] == '"')
  1239.         {
  1240.         fprintf(output_file, " \\\"");
  1241.         while (*++s != '"')
  1242.         {
  1243.             if (*s == '\\')
  1244.             {
  1245.             if (s[1] == '\\')
  1246.                 fprintf(output_file, "\\\\\\\\");
  1247.             else
  1248.                 fprintf(output_file, "\\\\%c", s[1]);
  1249.             ++s;
  1250.             }
  1251.             else
  1252.             putc(*s, output_file);
  1253.         }
  1254.         fprintf(output_file, "\\\"");
  1255.         }
  1256.         else if (s[0] == '\'')
  1257.         {
  1258.         if (s[1] == '"')
  1259.             fprintf(output_file, " '\\\"'");
  1260.         else if (s[1] == '\\')
  1261.         {
  1262.             if (s[2] == '\\')
  1263.             fprintf(output_file, " '\\\\\\\\");
  1264.             else
  1265.             fprintf(output_file, " '\\\\%c", s[2]);
  1266.             s += 2;
  1267.             while (*++s != '\'')
  1268.             putc(*s, output_file);
  1269.             putc('\'', output_file);
  1270.         }
  1271.         else
  1272.             fprintf(output_file, " '%c'", s[1]);
  1273.         }
  1274.         else
  1275.         fprintf(output_file, " %s", s);
  1276.     }
  1277.     if (!rflag) ++outline;
  1278.     fprintf(output_file, "\",\n");
  1279.     }
  1280.  
  1281.     if (!rflag) outline += 2;
  1282.     end_string_array(0);
  1283.     fprintf(output_file, "#endif\n");
  1284. }
  1285.  
  1286.  
  1287. #if __STDC__
  1288. static void output_stype(void)
  1289. #else
  1290. static void output_stype()
  1291. #endif
  1292. {
  1293.     if (!unionized && ntags == 0 && language == C)
  1294.     {
  1295.     outline += 3;
  1296.     fprintf(code_file, "#ifndef %sSTYPE\ntypedef int %sSTYPE;\n#endif\n",
  1297.         define_prefix, define_prefix);
  1298.     }
  1299. }
  1300.  
  1301.  
  1302. #if __STDC__
  1303. static void output_trailing_text(void)
  1304. #else
  1305. static void output_trailing_text()
  1306. #endif
  1307. {
  1308.     register int c, last;
  1309.     register FILE *in, *out;
  1310.  
  1311.     if (line == 0)
  1312.     return;
  1313.  
  1314.     in = input_file;
  1315.     out = code_file;
  1316.     c = *cptr;
  1317.     if (c == '\n')
  1318.     {
  1319.     ++lineno;
  1320.     if ((c = getc(in)) == EOF)
  1321.         return;
  1322.     if (!lflag)
  1323.     {
  1324.         ++outline;
  1325.         fprintf(out, line_format, lineno, input_file_name);
  1326.     }
  1327.     if (c == '\n')
  1328.         ++outline;
  1329.     putc(c, out);
  1330.     last = c;
  1331.     }
  1332.     else
  1333.     {
  1334.     if (!lflag)
  1335.     {
  1336.         ++outline;
  1337.         fprintf(out, line_format, lineno, input_file_name);
  1338.     }
  1339.     do { putc(c, out); } while ((c = *++cptr) != '\n');
  1340.     ++outline;
  1341.     putc('\n', out);
  1342.     last = '\n';
  1343.     }
  1344.  
  1345.     while ((c = getc(in)) != EOF)
  1346.     {
  1347.     if (c == '\n')
  1348.         ++outline;
  1349.     putc(c, out);
  1350.     last = c;
  1351.     }
  1352.  
  1353.     if (last != '\n')
  1354.     {
  1355.     ++outline;
  1356.     putc('\n', out);
  1357.     }
  1358.     if (!lflag)
  1359.     fprintf(out, line_format, ++outline + 1, code_file_name);
  1360. }
  1361.  
  1362.  
  1363. #if __STDC__
  1364. static void output_semantic_actions(void)
  1365. #else
  1366. static void output_semantic_actions()
  1367. #endif
  1368. {
  1369.     register int c, last;
  1370.     register FILE *out;
  1371.  
  1372.     fclose(action_file);
  1373.     action_file = fopen(action_file_name, "r");
  1374.     if (action_file == NULL)
  1375.     open_error(action_file_name);
  1376.  
  1377.     if ((c = getc(action_file)) == EOF)
  1378.     return;
  1379.  
  1380.     out = code_file;
  1381.     last = c;
  1382.     if (c == '\n')
  1383.     ++outline;
  1384.     putc(c, out);
  1385.     while ((c = getc(action_file)) != EOF)
  1386.     {
  1387.     if (c == '\n')
  1388.         ++outline;
  1389.     putc(c, out);
  1390.     last = c;
  1391.     }
  1392.  
  1393.     if (last != '\n')
  1394.     {
  1395.     ++outline;
  1396.     putc('\n', out);
  1397.     }
  1398.  
  1399.     if (!lflag)
  1400.     fprintf(out, line_format, ++outline + 1, code_file_name);
  1401. }
  1402.  
  1403.  
  1404. #if __STDC__
  1405. static void free_itemsets(void)
  1406. #else
  1407. static void free_itemsets()
  1408. #endif
  1409. {
  1410.     register core *cp, *next;
  1411.  
  1412.     FREE(state_table);
  1413.     for (cp = first_state; cp; cp = next)
  1414.     {
  1415.     next = cp->next;
  1416.     FREE(cp);
  1417.     }
  1418. }
  1419.  
  1420.  
  1421. #if __STDC__
  1422. static void free_shifts(void)
  1423. #else
  1424. static void free_shifts()
  1425. #endif
  1426. {
  1427.     register shifts *sp, *next;
  1428.  
  1429.     FREE(shift_table);
  1430.     for (sp = first_shift; sp; sp = next)
  1431.     {
  1432.     next = sp->next;
  1433.     FREE(sp);
  1434.     }
  1435. }
  1436.  
  1437.  
  1438.  
  1439. #if __STDC__
  1440. static void free_reductions(void)
  1441. #else
  1442. static void free_reductions()
  1443. #endif
  1444. {
  1445.     register reductions *rp, *next;
  1446.  
  1447.     FREE(reduction_table);
  1448.     for (rp = first_reduction; rp; rp = next)
  1449.     {
  1450.     next = rp->next;
  1451.     FREE(rp);
  1452.     }
  1453. }
  1454.